package com.kaifamiao.deno;

public class A {

    public static int getArr(int[] arr, int value) {
        int l = 0;
        int r = arr.length-1;
        while (l <= r) {
            int mid = (l + r) / 2;
            if (arr[mid] == value) {
                return mid;
            } else if (arr[mid] > value) {
                r = mid - 1;
            } else if (arr[mid] < value) {
                l = mid + 1;
            }
        }
        return -1;
    }

    public static void main(String[] args) {
        int[] arr = new int[]{1,2,3,4,5,6,7,8,9};
        int value = 0;
        int index = get(arr, 0, arr.length-1, value);
        if (index >= 0) {
            System.out.println(index);
        }else System.out.println("NO");
    }

    public static int get(int[] arr, int l, int r, int value) {
        int mid = (r + l) / 2;
        if (l > r) {
            return -1;
        }
        if (arr[mid] > value) {
            return get(arr, l, mid -1, value);
        } else if (arr[mid] < value) {
            return get(arr, mid + 1, r, value);
        } else {
            return mid;
        }
    }
}
